<template>
  <div>
    <el-row>
      <el-col :span="24">
        <div v-for="op in Object.keys(option)" :key="op">
          {{ op }}: {{ option[op] }}
        </div>

        <div class="grid-content">data: {{ data }}</div>
        <div class="grid-content">option: {{ option }}</div>
      </el-col>
    </el-row>
    <el-divider> .</el-divider>

    <el-row :gutter="20">
      <el-col :span="12">
        <div v-for="col in option.leftColumns" :key="col.name">
          {{ col.label }}: {{ getColValue(col) }}
        </div>
      </el-col>
      <el-col :span="12">
        <div v-for="col in option.rightColumns" :key="col.name">
          {{ col.label }}: {{ getColValue(col) }}
        </div>
      </el-col>
    </el-row>

    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane
        v-for="pane in option.tabs"
        :key="pane.name"
        :label="pane.label"
        :name="pane.name"
      >
        <div v-if="pane.type === 'card'">
          <el-row :gutter="20">
            <el-col
              v-for="rec in data[`${pane.column.name}__objects`]"
              :key="rec.id"
              :span="8"
            >
              <el-card class="box-card">
                {{ rec.name }}
              </el-card>
            </el-col>
          </el-row>
        </div>
        <div v-else>
          <el-row :gutter="20">
            <el-col v-for="col in pane.columns" :key="col.name" :span="12">
              {{ data[`${col.name}__objects`] }}
            </el-col>
          </el-row>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import odooConfig from '@/../odoo.config'

export default {
  name: 'ViewPage',
  components: {},
  directives: {},
  filters: {},

  props: {
    data: { type: Object, default: () => {} },
    option: { type: Object, default: () => {} }
  },

  data() {
    return {
      //

      activeName: '',

      debug: odooConfig.debug,

      loading: false,

      viewDialogVisible: false,

      formDialogVisible: false,
      formData: {},
      formRules: {
        // type: [{ required: true, message: 'type is required', trigger: 'change' }],
        // timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        // title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      }
    }
  },

  computed: {
    //
    firstTab: function() {
      const tabs = this.option.tabs
      if (tabs.length) {
        return tabs[0].name
      }

      return ''
    }
  },

  watch: {
    // firstTab(val) {
    //   console.log('option.tabs val', val)
    // }
  },
  async created() {
    //
    // console.log('option.tabs val', this.firstTab)
    this.activeName = this.firstTab
  },

  methods: {
    getColValue(col) {
      let colName = col.name

      if (col.type === 'select') {
        const tail = col.multiple ? 'names' : 'name'
        colName = `${col.name}__${tail}`
      }

      return this.data[colName]
    },

    handleClick(tab, event) {
      // console.log(tab, event)
    }
  }
}
</script>

<style>
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
